var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index:0,
    cate_list:[],
    is_cate_select:0,
    keyword:"",
    cate_logo:'',
    cate_title:''
  },
  keyword_action(e){
    this.setData({
      keyword:e.detail.value
    })
  },

  search_action(){
    this.package()
  },
  cate_select(e){
    this.setData({
      is_cate_select:e.currentTarget.dataset.id,
      cate_logo:e.currentTarget.dataset.item.logo,
      cate_title:e.currentTarget.dataset.item.title
    })
    this.package()
  },
  cate_none(){
    this.setData({
      is_cate_select:0,
      cate_id:0
    })
    this.package()
  },
  daohang_action(e){
    this.setData({
      index:e.currentTarget.dataset.id
    })
    getApp().globalData.nav = this.data.index
    this.package()
  },

  package(){
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: getApp().globalData.httpUrl+"api/mini/package",
      header: {
        'content-type': 'application/x-www-form-urlencoded','appid':getApp().globalData.appId
      },
      method: 'POST',
      dataType: 'json',
      data:{cate_id:that.data.is_cate_select,keyword:that.data.keyword,index:that.data.index},
      success: function(re) {
        wx.hideLoading()
        that.setData({
          package_list:re.data
        })
      }
    })
  },

  info(e){
    wx.navigateTo({
      url: '/pages/info/index?id='+e.currentTarget.dataset.id,
    })
  },

  insert_cart(e){
    var that = this
    wx.request({
      url: getApp().globalData.httpUrl+"api/mini/insert_cart",
      header: {
        'content-type': 'application/x-www-form-urlencoded','appid':getApp().globalData.appId
      },
      method: 'POST',
      dataType: 'json',
      data:{user_id:getApp().globalData.userInfo.id,id:e.currentTarget.dataset.id,project_id:e.currentTarget.dataset.projectid},
      success: function(re) {
        wx.showToast({
          title: '加入购物车成功',
        })
        app.cart_count()
      }
    })
  },

  cate(){
    var that = this
    wx.request({
      url: getApp().globalData.httpUrl+"api/mini/cate",
      header: {
        'content-type': 'application/x-www-form-urlencoded','appid':getApp().globalData.appId
      },
      method: 'POST',
      dataType: 'json',
      success: function(re) {
        console.log(re)
        that.setData({
          cate_list:re.data
        })
      }
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.cate()
    this.package()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      index:getApp().globalData.nav
    })
    this.package()
    app.cart_count()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})